<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item
              label="当前车牌号："
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="plateNumber"
            >
              <plate-number-input
                v-model="model.plateNumber"
                @change="handleInput"
              />
            </a-form-model-item>
          </a-col>

          <a-col :span="24">
            <a-form-model-item label="司机姓名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="driverName">
              <a-input v-model="model.driverName" placeholder="请输入司机姓名"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="联系电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="driverPhone" :rules="rules.driverPhone">
              <a-input v-model="model.driverPhone" placeholder="请输入联系电话"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="进厂目的" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="purpose">
              <j-dict-select-tag type="list" v-model="model.purpose" dictCode="driver_purpose" placeholder="请选择进厂目的" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="预约进厂时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="reservationTime">
              <j-date placeholder="请选择预约进厂时间"  v-model="model.reservationTime" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </a-form-model-item>
          </a-col>

          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-input v-model="model.remark" placeholder="请输入备注"  ></a-input>
            </a-form-model-item>
          </a-col>

        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import PlateNumberInput from '@views/srm/materialstore/modules/PlateNumberInput.vue'

  export default {
    name: 'BussCarReservationInfoForm',
    components: {
      PlateNumberInput
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        rules: {
          driverPhone: [
            { required: true, message: '请输入联系电话', trigger: 'blur' },
            {
              pattern: /^1[3-9]\d{9}$/,
              message: '请输入11位手机号码',
              trigger: 'blur'
            }
          ]
        },
        model:{

          plateNumber: '', // 确保 model 中有 plateNumber 字段
          province: '', // 确保 model 中有 province 字段
          plateBody: '', // 确保 model 中有 plateBody 字段

         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/bussCarReservationInfo/bussCarReservationInfo/add",
          edit: "/bussCarReservationInfo/bussCarReservationInfo/edit",
          queryById: "/bussCarReservationInfo/bussCarReservationInfo/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },

      handleInput(val) {
        console.log('车牌号变更11:', val)
        this.model.plateNumber = val.plateNumber; // 确保值正确更新
        this.model.province = val.province;
        this.model.plateBody = val.plateBody;
      },

    }
  }
</script>